package com.lvhx.controller;

import com.lvhx.dao.OrderMapper;
import com.lvhx.dao.UserMapper;
import com.lvhx.dao.extend.OrderExtednMapper;
import com.lvhx.ehcache.CacheRepository;
import com.lvhx.pojo.Order;
import com.lvhx.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

/**
 * @auther 吕宏旭
 * @date 2019/8/24 13:53
 * http://localhost:9999/swagger-ui.html#/
 */
@Slf4j
@Api(tags = "2.SpringBoot整合Redis-支持系统版本")
@RestController
@RequestMapping("/order")
@Transactional
public class RedisTestController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CacheRepository cacheRepository;

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private OrderExtednMapper orderExtednMapper;

    @ApiOperation("查找所有订单")
    @RequestMapping(value = {"/findAll"}, produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public List<Order> findAll() throws SQLException {
        System.out.println("进入findAll-beforstart");
        List<Order> list = orderService.findAllOreder();
        System.out.println("进入findAll-end");
        return list;
    }


    @ApiOperation("测试插入数据")
    @RequestMapping(value = {"/testInsert"}, method = RequestMethod.GET)
    public void testInsert() {
        Order order = new Order();
        order.setId(String.valueOf(System.currentTimeMillis()));
        order.setName(String.valueOf(System.currentTimeMillis()));
        order.setMessageId(String.valueOf(System.currentTimeMillis()));
        orderService.testInsert(order);
    }


    @ApiOperation("根据id更新订单")
    @RequestMapping(value = {"/updateOrder/{id}"}, method = RequestMethod.POST)
    @Transactional
    public void updateOrder(@PathVariable String id) {
        Order order1 = orderService.findOrderById("1");
        order1.setName(UUID.randomUUID().toString().substring(0, 10));
        order1.setMessageId(UUID.randomUUID().toString().substring(0, 10));
        orderService.updateOrder(order1);


        Order order2 = orderService.findOrderById("2");
        order2.setName(UUID.randomUUID().toString().substring(0, 10));
        order2.setMessageId(UUID.randomUUID().toString().substring(0, 10));
        orderService.updateOrder(order2);

        Order order3 = orderService.findOrderById("3");
        order3.setName(UUID.randomUUID().toString().substring(0, 10));
        order3.setMessageId(UUID.randomUUID().toString().substring(0, 10));
        orderService.updateOrder(order3);
    }


    @RequestMapping(value = {"/清理所有缓存"}, method = RequestMethod.GET)
    public void clearCache() {
        orderService.clearCache();
    }


    @ApiOperation("走ehcache缓存查找所有用户")
    @RequestMapping(value = {"/findAllByEhcache"}, produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String findAllByEhcache() {
        String allOrder = cacheRepository.selectAllUser();
        return allOrder;
    }


    @ApiOperation("根据ID查询订单")
    @RequestMapping(value = {"/getOrderById/{id}"}, produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getOrderById(@PathVariable String id) {
        StopWatch stopWatch = new StopWatch("订单处理");
        stopWatch.start();
//        Order order = orderExtednMapper.getOrderById(id);
        Order order = orderMapper.selectByPrimaryKey(id);
//        order.setMessageId("test");
//        order.setId("A");
        stopWatch.stop();
        log.info("=====根据ID查询订单耗时统计:\n{}", stopWatch.prettyPrint());
        return order.toString();

//        return orderMapper.selectByPrimaryKey(id).toString();
    }


    @ApiOperation("测试redis信息")
    @RequestMapping(value = {"/testRedis"}, produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String testRedis() {

        Properties info = stringRedisTemplate.getConnectionFactory().getConnection().info("server");
        String run_id = info.getProperty("run_id");
        return null;
    }

}
